// Copyright tang.  All rights reserved.
// https://gitee.com/inrgihc/dbswitch
//
// Use of this source code is governed by a BSD-style license
//
// Author: tang (inrgihc@126.com)
// Date : 2020/1/2
// Location: beijing , china
/////////////////////////////////////////////////////////////
package com.sondertara.joya.migration.provider;


import com.sondertara.common.base.Assert;
import com.sondertara.joya.core.jdbc.support.DbType;
import com.sondertara.joya.core.jdbc.support.dialect.DialectFactory;
import com.sondertara.joya.core.jdbc.support.dialect.SqlDialect;
import com.sondertara.joya.core.jdbc.support.dialect.SqlDialectRegister;
import com.sondertara.joya.exceptions.DbException;

import javax.sql.DataSource;
import java.sql.Connection;

public abstract class AbstractFactoryProvider implements ProductFactoryProvider {

    private DataSource dataSource;
    private DbType dbType;
    private SqlDialect sqlDialect;

    protected AbstractFactoryProvider(DataSource dataSource) {
        Assert.notNull(dataSource);
        this.dataSource = dataSource;
        try (Connection connection = dataSource.getConnection()) {
            this.dbType = DialectFactory.getDatabaseType(connection);
            sqlDialect=SqlDialectRegister.findOrCreate(this.dbType);
        } catch (Exception e) {
            throw new DbException(e);
        }

    }

    public DataSource getDataSource() {
        return this.dataSource;
    }

    @Override
    public SqlDialect getSqlDialect() {
        return sqlDialect;
    }

    @Override
    public final DbType getProductType() {
        return this.dbType;

    }

}
